// src/utils/mockData.ts
export interface Session {
  id: number
  name: string
  lastMessage: string
}

export interface Message {
  id: number
  sessionId: number
  from: string
  type: 'text' | 'emoji' | 'file' | 'image' | 'system'
  content: string  // 文本/emoji 字符串，或文件名，或 base64
  time: string
}

// 会话列表不变
export const sessions: Session[] = [
  { id: 1, name: 'Alice', lastMessage: 'Hello!' },
  { id: 2, name: 'Vue Group', lastMessage: 'Welcome to Vue Chat' },
]

// 用 let 而不是 const，方便 push
export let allMessages: Message[] = [
  { id: 1, sessionId: 1, from: 'Alice',   type: 'text',  content: 'Hi there!',           time: '10:00' },
  { id: 2, sessionId: 1, from: 'You',     type: 'text',  content: 'Hello Alice!',       time: '10:01' },
  { id: 3, sessionId: 2, from: 'Bob',     type: 'text',  content: 'Hey everyone 👋',     time: '09:30' },
  { id: 4, sessionId: 2, from: 'Charlie', type: 'text',  content: 'Let’s start coding.', time: '09:32' },
]

/** 根据会话 ID 取历史消息 */
export function getMessages(sessionId: number): Message[] {
  return allMessages.filter(m => m.sessionId === sessionId)
}

/** 添加新消息 */
export function addMessage(msg: Omit<Message, 'id' | 'time'>): void {
  const newId = allMessages.length
    ? Math.max(...allMessages.map(m => m.id)) + 1
    : 1
  const time = new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })
  allMessages.push({ id: newId, time, ...msg })
}
